from django.forms import model_to_dict
from django.http import HttpResponse, JsonResponse
from website_server.utils import http_post, log_in_admin, model_to_list
from website_server.models import ArticleTab, LoginTab, ReportCommentsTab, ReportTopicsTab
from website_server.views import SystemInformsClass

# Create your views here.
# 登录
@http_post
def toLogin(request):
  if request.session.get('admin'):
    return HttpResponse(1)
  rq = request.POST
  username = rq.get('username')
  password = rq.get('password')
  if username and password:
    user = LoginTab.objects.filter(username=username,password=password).first()
    if user and user.root == 1:
      request.session['admin'] = username
      request.session.set_expiry(0)
      return HttpResponse(1)
    else:
      return HttpResponse('用户名密码错误!')
  else:
    return HttpResponse('用户名密码不能为空!')

# 获取未审核文章
@http_post
def getAuditArticle(request):
  article = ArticleTab.objects.filter(state='待审核').order_by('time')
  return JsonResponse({'data':model_to_list(article)})

# 提交审核结果
@http_post
def submitAudit(request):
  rq = request.POST
  classify = rq.get('classify')
  article = ArticleTab.objects.filter(articleid=rq.get('id')).first()
  if article:
    cause = ''
    if classify:
      article.state = '已通过'
      article.classify = classify
      SystemInformsClass(username=article.username,obj=article,cause=cause).create(101)
    else:
      cause = rq.get('cause')
      article.state = '未通过'
      SystemInformsClass(username=article.username,obj=article,cause=cause).create(102)
    article.save()
    
    return HttpResponse(1)
  else:
    return HttpResponse('参数错误')

# 获取举报数据
@http_post
def getReportData(request):
  reportTopics = ReportTopicsTab.objects.all().order_by('time')
  report_topics = [{
    **model_to_dict(x),
    'content': x.topicsid.content
  }for x in reportTopics]
  reportComments = ReportCommentsTab.objects.all().order_by('time')
  report_comments = [{
    **model_to_dict(x),
    'content': x.commentid.content
  }for x in reportComments]
  return JsonResponse({'topics':report_topics,'comments':report_comments})

# 删除举报信息
@log_in_admin
def removeReport(request):
  rq = request.POST
  id = rq.get('id')
  if rq.get('title'):
    if rq.get('title') == '话题':
      obj = ReportTopicsTab.objects.filter(reportid=id).first()
      if obj:
        obj.delete()
        SystemInformsClass(username=obj.username,obj=obj,cause='话题').create(104)
    elif rq.get('title') == '评论':
      obj = ReportCommentsTab.objects.filter(reportid=id).first()
      if obj:
        obj.delete()
        SystemInformsClass(username=obj.username,obj=obj,cause='评论').create(104)
    return HttpResponse(1)
  else:
    return HttpResponse('参数错误')

# 是否置顶文章
@log_in_admin
def isTop(request):
  rq = request.POST
  id = rq.get('id')
  if id:
    article = ArticleTab.objects.filter(articleid=id).first()
    article.istop = 1 if article.istop != 1 else 0
    article.save()
    return HttpResponse(article.istop)
  else:
    return HttpResponse('文章不存在')